package hg.demo.mybatis.plus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hg.demo.mybatis.plus.condition.UserCondition;
import hg.demo.mybatis.plus.entity.UserEntity;
import hg.demo.mybatis.plus.mapper.UserMapper;
import hg.demo.mybatis.plus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service("hgUserService")
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Override
    public UserEntity query(String userName) {
        UserCondition condition = new UserCondition();
        condition.setUserName(userName);
        return single(condition);
    }

    private UserEntity single(UserCondition condition) {
        List<UserEntity> dataList = list(condition);
        return (null == dataList || dataList.isEmpty() ? null : dataList.get(0));
    }

    private List<UserEntity> list(UserCondition condition) {
        try {
            return this.baseMapper.list(condition);
        } catch (Exception e) {
            log.error("list(),查询,errMsg:{}", e.getMessage(), e);
            return new ArrayList<>(0);
        }
    }
}
